package com.construction.api.controller.usual;

import com.construction.api.domain.model.result.PageResult;
import com.construction.api.service.IService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 通用的增删查改接口
 * T 数据库表entity
 * E 接口返回的实体内容
 * K 数据库主键类型
 * 接口传入的参数范围为T,返回的结果参数范围为E, 可以过滤掉部分不让前端查看的字段
 *
 * @author dybin
 * @Date: 2021-07-04 13:23
 */
@RequiredArgsConstructor
public class UsualController<T, K, E> extends AbstractController<T, K, E> {

    protected final IService<T, K, E> iService;

    @GetMapping("/{id}")
    @Override
    public E get(@PathVariable("id") K id) {
        return iService.get(id);
    }

    @PostMapping("")
    @Override
    public E insert(@RequestBody @Valid T t) {
        return iService.insert(t);
    }

    @PutMapping("/{id}")
    @Override
    public E update(@PathVariable("id") K k, @RequestBody @Valid T t) {
        return iService.update(k, t);
    }

    @DeleteMapping("/{id}")
    @Override
    public void delete(@PathVariable("id") K k) {
        iService.delete(k);
    }

    @GetMapping("/list")
    @Override
    public List<E> queryList(T t) {
        return iService.queryList(t);
    }

    @GetMapping("page-list")
    @Override
    public PageResult<E> queryPageList(@RequestParam(required = false, defaultValue = "10") int size,
                                       @RequestParam(required = false, defaultValue = "1") int current,
                                       T t) {
        return iService.queryPageList(size, current, t);
    }
}
